import request from "@/utils/request";
import type { AxiosPromise } from "axios";
import type { LoginData, LoginResult, VerifyCodeResult } from "./types";

// pc端固定客户端授权id
const clientId = import.meta.env.VITE_APP_CLIENT_ID;

/**
 * 获取验证码
 */
export function getCodeImg(): AxiosPromise<VerifyCodeResult> {
  return request({
    url: "/auth/code",
    headers: {
      isToken: false,
    },
    method: "get",
    timeout: 20000,
  });
}

/**
 * @param data {LoginData}
 * @returns
 */
export function login(data: LoginData): AxiosPromise<LoginResult> {
  const params = {
    ...data,
    tenantId: "000000",
    clientId: data.clientId || clientId,
    grantType: data.grantType || "password",
  };
  return request({
    url: "/auth/login",
    headers: {
      isToken: false,
      isEncrypt: true,
      repeatSubmit: false,
    },
    method: "post",
    data: params,
  });
}

/**
 * 注销
 */
export function logout() {
  return request({
    url: "/auth/logout",
    method: "post",
  });
}
